package com.dartsstats;

import java.util.List;

import com.dartsstats.R;
import com.dartsstats.R.id;
import com.dartsstats.R.layout;
import com.dartsstats.datasource.PlayerDataSource;
import com.dartsstats.datasource.TeamDataSource;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class PlayerListActivity extends Activity {
	
	private Team team;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		long teamID = -1;
		if(extras != null) {
			teamID = extras.getLong("TEAM_ID");
			team = new TeamDataSource(this).getTeamByID((int)teamID);
		}
		setContentView(R.layout.activity_player_list);
		createView();
	}

	private void createView() {
		long teamID = team.getId();
		List<Player> players = new PlayerDataSource(this).getAllPlayersForTeam((int)teamID);
		Spinner spinner = (Spinner) findViewById(R.id.playerName);
//		List<String> playerNames = new ArrayList<String>();
//		for(Player player : players) {
//			playerNames.add(player.getFirstName() + " " + player.getLastName());
//		}
		ArrayAdapter<Player> adapter = new ArrayAdapter<Player>(this,
				android.R.layout.simple_spinner_item, players);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		TextView teamNameTextView = (TextView) findViewById(R.id.teamNameLabel);
		teamNameTextView.setText(team.getName());
	}
	
	public void addNewPlayer(View view) {
		PlayerDataSource playerDataSource = new PlayerDataSource(this);
		EditText firstNameText = (EditText) findViewById(R.id.newPlayerFirstName);
		String newPlayerFirstName = firstNameText.getText().toString();
		EditText lastNameNext = (EditText) findViewById(R.id.newPlayerLastName);
		String newPlayerLastNme = lastNameNext.getText().toString();
		playerDataSource.createPlayer(newPlayerFirstName, newPlayerLastNme, (int)team.getId(), "", "");
		finish();
		startActivity(getIntent());
	}
	
	public void editPlayer(View view) {
		Intent intent = new Intent(this, EditIndividualPlayerActivity.class);
		Spinner playerSpinner = (Spinner) findViewById(R.id.playerName);
		Player player = (Player) playerSpinner.getSelectedItem();
		long playerID = player.getId();
		intent.putExtra("PLAYER_ID", playerID);
    	startActivity(intent);
	}

}
